<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="身份证号" prop="userIdCard">
        <el-input
          v-model="queryParams.userIdCard"
          placeholder="请输入人员身份证号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车牌号码" prop="carLicenceNumber" v-hasRole="['admin']">
        <el-input
          v-model="queryParams.carLicenceNumber"
          placeholder="请输入车牌号码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="认证状态" prop="isValid" v-hasRole="['admin']">
        <el-select v-model="queryParams.isValid" placeholder="选择认证状态" clearable size="small">
          <el-option
            v-for="dict in dict_isValid"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!--<el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:car:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:car:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:car:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:car:export']"
        >导出</el-button>
      </el-col>
    </el-row>-->

    <el-table v-loading="loading" :data="carList" :max-height="tableHeight" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="身份证号" align="center" prop="userIdCard" width="170"/>
      <el-table-column label="车牌号码" align="center" prop="carLicenceNumber" />
      <el-table-column label="车牌颜色" align="center" prop="carLicenceColor" :formatter="carColorFormat" />
      <el-table-column label="车辆类型" align="center" prop="carType" :formatter="carTypeFormat"/>
      <!--<el-table-column label="核定载重量(kg)" align="center" prop="checkCarryingCapacity" />
      <el-table-column label="整备质量(kg)" align="center" prop="unladenMass" />-->
      <el-table-column label="核定总质量(kg)" align="center" prop="carTotal" />
      <el-table-column label="准牵引总质量(kg)" align="center" prop="tractionMass" width="130"/>
      <el-table-column label="车轴数" align="center" prop="carAxleNums" />
      <el-table-column label="道路运输证号" align="center" prop="transportCertificateNum" width="160"/>
      <!--<el-table-column label="道路运输证有效期" align="center" prop="transportationValidDate">
        <template slot-scope="scope">
          <span v-if="scope.row.transportationValidDate != null">{{ scope.row.transportationValidDate | dateString('YYYY-MM-DD')  }}</span>
        </template>
      </el-table-column>-->

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="140">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >详情</el-button>
          <!--<el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:car:remove']"
          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改人员车辆对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="980px">
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-col :span="12">
            <el-form-item label="人员身份证号" >{{form.userIdCard}}</el-form-item>
            <el-form-item label="车牌号码">{{form.carLicenceNumber}}</el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="车牌颜色" >{{form.carLicenceColorName}}</el-form-item>
          <el-form-item label="车辆类型">{{form.carTypeName}}</el-form-item>
       </el-col>

        <el-col :span="12">
            <el-form-item label="道路运输证号" >{{form.transportCertificateNum}} </el-form-item>
            <el-form-item label="是否当前使用车">{{isYesOrNo(form.isCurrentUse)}}</el-form-item>
        </el-col>

        <el-col :span="12">
            <el-form-item label="是否已绑定挂车" >{{isYesOrNo(form.hasTrailerCar)}}</el-form-item>
            <el-form-item label="当前绑定挂车编号" >{{form.trailerCarNo}}</el-form-item>
        </el-col>


          <el-col :span="12">
          <el-form-item label="核定载重量" >{{form.checkCarryingCapacity}}
          </el-form-item>
          </el-col>
          <el-col :span="12">
          <el-form-item label="车轴数" >{{form.carAxleNums}}

          </el-form-item>
          </el-col>



            <el-col :span="12">
           <el-form-item label="整备质量" prop="unladenMass">
             {{form.unladenMass}}
          </el-form-item>
            </el-col>
            <el-col :span="12">
          <el-form-item label="车辆总质量" >
           {{form.carTotal}}
          </el-form-item>
            </el-col>

        <el-row>
          <el-col :span="12">
          <el-form-item label="道路运输证有效期:" >
            {{form.transportationValidDate}}
          </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="准牵引总质量" prop="tractionMass">
             {{form.tractionMass}}
             </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="行驶证主页照片" v-if="form.vehicleFirstPageUrl != null && form.vehicleFirstPageUrl != ''">
              <el-image :src="form.vehicleFirstPageUrl" style="width: 100px; height: 100px" :preview-src-list="srcList">
                <div slot="error" class="image-slot">
                  暂无图片
                </div>
              </el-image>
            </el-form-item>
            <el-form-item label="行驶证主页照片" v-else>
              暂无图片
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="行驶证副页照片" v-if="form.vehicleSecondPageUrl != null && form.vehicleSecondPageUrl != ''">
              <el-image :src="form.vehicleSecondPageUrl" style="width: 100px; height: 100px" :preview-src-list="srcList" >
                <div slot="error" class="image-slot">
                  暂无图片
                </div>
              </el-image>
            </el-form-item>
            <el-form-item label="行驶证副页照片" v-else>
              暂无图片
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-form-item label="道路运输证照片"  v-if="form.transportationPageUrl != null && form.transportationPageUrl != ''">
            <el-image :src="form.transportationPageUrl" style="width: 100px; height: 100px" :preview-src-list="srcList" >
              <div slot="error" class="image-slot">
                暂无图片
              </div>
            </el-image>
          </el-form-item>
          <el-form-item label="道路运输证照片" v-else>
            暂无图片
          </el-form-item>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 车型校验 -->
    <el-dialog title="车型校验比对结果" :visible.sync="openView" width="800px">
      <el-form ref="form" :model="form"  label-width="110px">
        <div class="div-view">
          <table class="sheet_table">
            <tr>
              <td width="33%;" ></td>
              <td width="33%;" >标准</td>
              <td width="34%;" >实际</td>
            </tr>
            <tr>
              <td>车辆类型</td>
              <td>{{this.car.type}}</td>
              <td>{{form.carTypeName}}</td>
            </tr>
            <tr>
              <td >总质量</td>
              <td >{{this.car.allMass}}</td>
              <td>{{form.carTotal}}</td>
            </tr>
            <tr>
              <td >整备质量</td>
              <td >{{this.car.curbWeight}}</td>
              <td>{{form.unladenMass}}</td>
            </tr>
            <tr>
              <td >核定载重量</td>
              <td >{{this.car.ratedMass}}</td>
              <td>{{form.checkCarryingCapacity}}</td>
            </tr>
            <tr>
              <td >准牵引总质量</td>
              <td >{{this.car.totalMassOfQuasiTrailer}}</td>
              <td>{{form.tractionMass}}</td>
            </tr>
            <tr>
              <td>车轴数</td>
              <td>{{this.car.axleNumber}}</td>
              <td>{{form.carAxleNums}}</td>
            </tr>
          </table>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="openView = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCar, getCar, delCar, addCar, updateCar, exportCar } from "@/api/weight/car";

export default {
  name: "Car",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 人员车辆表格数据
      carList: [],
      // 弹出层标题
      title: "",

      //认证状态
      dict_isValid: [],

      dict_authenticationtype: [],

      dict_cartype: [],
      dict_carcolor: [],
      srcList: [],

      // 是否显示弹出层
      open: false,
      openView: false,
      station_btn: false,
      tableHeight: window.innerHeight - 275,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userIdCard: undefined,
        carType: undefined,
        carLicenceNumber: undefined,
        carLicenceColor: undefined,
        carAxleNums: undefined,
        transportCertificateNum: undefined,
        checkCarryingCapacity: undefined,
        carTotal: undefined,
        tractionMass: undefined,
        vehicleFirstPageUrl: undefined,
        vehicleSecondPageUrl: undefined,
        transportationPageUrl: undefined,
        isCurrentUse: undefined,
        hasTrailerCar: undefined,
        trailerCarId: undefined,
        isValid: undefined,
        validReason: undefined,
        status: undefined,
        carTypeName: undefined,
        carLicenceNumberName: undefined,
        trailerCarNo: undefined,
      },
      // 表单参数
      form: {
      },
      car:{
        type: undefined,
        axleNumber: undefined,
        curbWeight: undefined,
        allMass: undefined,
        ratedMass: undefined,
        totalMassOfQuasiTrailer: undefined,
      },
      // 表单校验
      rules: {
        userIdCard: [
          { required: true, message: "人员身份证号不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();

    // 认证状态
    this.getDicts("is_valid").then(response => {
      this.dict_isValid = response.data;
    });

    this.getDicts("authentication_type").then(response => {
      this.dict_authenticationtype = response.data;
    });
    this.getDicts("car_type").then(response => {
      this.dict_cartype = response.data;
    });
    this.getDicts("car_color").then(response => {
      this.dict_carcolor = response.data;
    });

    const roles = this.$store.getters && this.$store.getters.roles;
    for (let rol of roles){
      if ('station'==rol || 'hs_station'==rol){
        this.station_btn=true;
      }
    }

  },
  methods: {
    /** 查询人员车辆列表 */
    getList() {
      this.loading = true;
      listCar(this.queryParams).then(response => {
        this.carList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        carId: undefined,
        userIdCard: undefined,
        carType: undefined,
        carLicenceNumber: undefined,
        carLicenceColor: undefined,
        carAxleNums: undefined,
        transportCertificateNum: undefined,
        checkCarryingCapacity: undefined,
        carTotal: undefined,
        vehicleFirstPageUrl: undefined,
        vehicleSecondPageUrl: undefined,
        transportationPageUrl: undefined,
        isCurrentUse: undefined,
        hasTrailerCar: undefined,
        trailerCarId: undefined,
        isValid: undefined,
        validReason: undefined,
        status: "0",
        delFlag: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        tractionMass: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.carId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加人员车辆";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const carId = row.carId || this.ids
      getCar(carId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "车辆详情";

        this.srcList.push(this.form.vehicleFirstPageUrl);
        this.srcList.push(this.form.vehicleSecondPageUrl);
        this.srcList.push(this.form.transportationPageUrl);
      });
    },
    /** 详情 **/
    handleView(row) {
      this.reset();
      const carId = row.carId || this.ids
      getCar(carId).then(response => {
        this.form = response.data;
        if (response.data.car === null){
          this.$message.warning('标准中无该车型！')
        }else {
          this.car = this.form.car;
          this.openView = true;
        }
      });
    },
    // 认证状态
    isValidFormat(row,column){
      return this.selectDictLabel(this.dict_isValid, row.isValid);
    },
    authenticationtypeFormat(row,column){
      return this.selectDictLabel(this.dict_authenticationtype, row.authenticationtype);
    },
    carTypeFormat(row,column){
      return this.selectDictLabel(this.dict_cartype, row.carType);
    },
    carColorFormat(row,column){
      return this.selectDictLabel(this.dict_carcolor, row.carLicenceColor);
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.carId != undefined) {
            let tp_form={
              carId: this.form.carId,
              checkCarryingCapacity: this.form.checkCarryingCapacity,
              carAxleNums: this.form.carAxleNums,
              unladenMass: this.form.unladenMass,
              tractionMass: this.form.tractionMass,
              carTotal: this.form.carTotal,
              transportationValidDate: this.form.transportationValidDate,
              transportationState: this.form.transportationState,
              checkCarryingCapacity: this.form.checkCarryingCapacity,
              isValid: this.form.isValid,
              validReason: this.form.validReason
            };
            updateCar(tp_form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addCar(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const carIds = row.carId || this.ids;
      this.$confirm('是否确认删除人员车辆编号为"' + carIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCar(carIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有人员车辆数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportCar(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },
    isYesOrNo(value) {
      if(value == '1'){
        return '是';
      }else{
        return '否';
      }
    }
  }
};
</script>
<style lang="scss" scoped>
  @import '../../../assets/styles/ledger_style.scss';
</style>
